//
//  MBProgressHUD+Extension.m
//  HHNMobileFramework
//
//  Created by huhmf on 16/2/20.
//  Copyright © 2016年 huhmf. All rights reserved.
//

#import "MBProgressHUD+Extension.h"

@implementation MBProgressHUD (Extension)

+ (instancetype)ShowHUDinView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud =[[MBProgressHUD alloc] initWithView:view];
    hud.animationType = MBProgressHUDAnimationZoom; // 默认动画样式
    hud.removeFromSuperViewOnHide = YES; // 该视图隐藏后则自动从父视图移除掉
    
    hud.opacity		 = 0.7f;	// 设定透明度
    hud.cornerRadius	= 5.f;	 // 设定圆角
    hud.margin	  = 10.f;
    hud.labelFont=[UIFont systemFontOfSize:16];
    [view addSubview:hud];
    
    return hud;
}

+ (void)showTextOnly:(NSString *)text
     configParameter:(ConfigShowHUDBlock)config
            duration:(NSTimeInterval)sec
              inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    hud.labelText = text;
    hud.mode = MBProgressHUDModeText;
    
    // 配置额外的参数
    config(hud);
    // 显示
    [hud show:YES];
    if (sec>0) {
        // 延迟sec后消失
        [hud hide:YES afterDelay:sec];
    }
}

+ (void)showText:(NSString *)text
 configParameter:(ConfigShowHUDBlock)config
        duration:(NSTimeInterval)sec
          inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    hud.labelText = text;
    
    // 配置额外的参数
    config(hud);
    // 显示
    [hud show:YES];
    if (sec>0) {
        // 延迟sec后消失
        [hud hide:YES afterDelay:sec];
    }
}

+ (void)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock
       configParameter:(ConfigShowHUDBlock)config
              duration:(NSTimeInterval)sec
                inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    
    // 配置额外的参数
    config(hud);
    
    // 自定义View
    hud.customView   = viewBlock();
    
    // 显示
    [hud show:YES];
    
    if (sec>0) {
        // 延迟sec后消失
        [hud hide:YES afterDelay:sec];
    }
}



+ (instancetype)showTextOnly:(NSString *)text
             configParameter:(ConfigShowHUDBlock)config
                      inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    hud.labelText = text;
    hud.mode = MBProgressHUDModeText;
    
    // 配置额外的参数
    config(hud);
    // 显示
    [hud show:YES];
   
    
    return hud;
}
+ (instancetype)showText:(NSString *)text
         configParameter:(ConfigShowHUDBlock)config
                  inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    hud.labelText = text;
    
    // 配置额外的参数
    config(hud);
    // 显示
    [hud show:YES];

    
    return hud;
}
+ (instancetype)showCustomView:(ConfigShowHUDCustomViewBlock)viewBlock
               configParameter:(ConfigShowHUDBlock)config
                        inView:(UIView *)view
{
    MBProgressHUD *hud=[self ShowHUDinView:view];
    
    // 配置额外的参数
    config(hud);
    
    // 自定义View
    hud.customView   = viewBlock();
    
    // 显示
    [hud show:YES];
    
    return hud;
}

+ (void)showSuccess:(NSString *)success
{
    [self showTextOnly:success configParameter:^(MBProgressHUD *config) {
        
    } duration:2.0f inView:nil];
}
+ (void)showError:(NSString *)error
{
    [self showTextOnly:error configParameter:^(MBProgressHUD *config) {
        
    } duration:2.0f inView:nil];
}

+(void)showTextOnly:(NSString *)text toView:(UIView *)view
{
    [self showTextOnly:text configParameter:^(MBProgressHUD *config) {
        
    } duration:2.0f inView:view];
}

+ (void)hideHUDForView:(UIView *)view
{
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
@end
